package com.heima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.heima.constant.MessageConstant;
import com.heima.entity.PageResult;
import com.heima.entity.QueryPageBean;
import com.heima.entity.Result;
import com.heima.pojo.CheckItem;
import com.heima.serivce.CheckItemSerivice;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemsController {

    @Reference
    private CheckItemSerivice checkItemSerivice;

    /*添加检查项*/
    @PostMapping
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemSerivice.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    /*根据查询条件分页查询*/
    @GetMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkItemSerivice.findPage(queryPageBean);
    }

    /*根据id删除检查项*/
    @DeleteMapping("/delete.do")
    public Result delete(@RequestParam("id") Integer id){
        try {
            checkItemSerivice.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    /*修改检查项信息*/
    public Result edit(CheckItem checkItem){
        try {
            checkItemSerivice.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /*根据id来查询检查项*/
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemSerivice.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /*查询所有*/
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> all = checkItemSerivice.findAll();
        if (all != null && all.size() != 0) {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, all);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
